#set table = $tabdict.table
#set tablep = $tabdict.tablep	
#set Table = $tabdict.Table
#set pk = $tabdict.pk
#set PTable = $tabdict.PTable
#set propertys = $tabdict.propertys
#set type = $tabdict.type
package com.kylin.module.${type}.orm;

import com.kylin.hibernate.BaseHibernateDAO;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.kylin.common.exception.BusinessException;
import com.kylin.common.exception.SystemException;

/**
 * Data access object (DAO) for domain model class Testcompany.
 * @see com.kylin.module.test.orm.Testcompany
 * @author liangzh
 */
public class ${Table}DAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(${Table}DAO.class);

//    


	public Integer getTotal(){
        try {
            String queryString = "select count(*) from ${Table}  where   active=1";        	   
            Query query = getSession().createQuery(queryString);
   		 	//query.setParameter(0, value);
            //log.info("${table.lower()}size: "+ query.list().size());
   		 	return (Integer)query.list().get(0);
         } catch (RuntimeException re) {
            log.error("get ${table.lower()} total", re);
            throw re;
         }		
	}
    
	public List getList(Integer limitstart, Integer limit){
        log.info("finding ${table.lower()} " + limitstart + " " + limit );
        try {
           String queryString = "from ${Table}   where   active=1 order by  createdon desc";        	   
           Query query = getSession().createQuery(queryString);
           //query.setEntity(0, user);
           query.setFirstResult(limitstart);
           query.setMaxResults(limit);
  		 //query.setParameter(0, value);
           log.info("size: "+ query.list().size());
  		 return query.list();
        } catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
        }
		
	}

    public void save(${Table} transientInstance) {
        log.debug("${Table} instance");
        try{
        	super.save(transientInstance);
        }
        catch (BusinessException be){
        	
        }
    }
    
	public void delete(${Table} persistentInstance) {
        log.debug("deleting ${Table} instance");
        try{
	        super.delete(persistentInstance);
	    }
	    catch (BusinessException be){
	    }

    }
    
    
    public List findByType(String condition){
        log.debug("${Table}: findByType" + condition); 
        return null;   	
    }
    
    
    public ${Table} findById( java.lang.Long id) {
        log.debug("getting ${Table} instance with id: " + id);
        try{
	        return (${Table})findById(${Table}.class , id);
	    }
	    catch (BusinessException be){
	    	return null;
	    }
    }
    
    
    public List findByExample(${Table} instance) {
        log.debug("finding ${Table} instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.kylin.module.${table}.orm.${Table}")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ${Table} instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ${Table} as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

    public ${Table} merge(${Table} detachedInstance) {
        log.debug("merging ${Table} instance");
        try {
            ${Table} result = (${Table}) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(${Table} instance) {
        log.debug("attaching dirty ${Table} instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(${Table} instance) {
        log.debug("attaching clean ${Table} instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}